package com.j2some;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.RequestGongWuShu.vehicle;
import com.alibaba.fastjson.JSONObject;
import com.yinquan.util.DataUtil;
import com.yinquan.util.ImagesAndBase64Util;
import com.yishun.springboot.Application;
import com.yishun.springboot.mapper.GwsVehicleMasterDetailedMapper;
import com.yishun.springboot.mapper.PinyiVehicleDetailedMapper;
import com.yishun.springboot.mapper.WeiZhongYunVehicleDetailedMapper;
import com.yishun.springboot.mapper.WeiZhongYunVehicleGetDataMapper;
import com.yishun.springboot.pojo.GwsVehicleMasterDetailed;
import com.yishun.springboot.pojo.PinyiVehicleDetailed;
import com.yishun.springboot.pojo.WeiZhongYunVehicleDetailed;
import com.yishun.springboot.pojo.WeiZhongYunVehicleGetData;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 手动原始操作上传车辆数据
 * 
 * @date 2021年3月22日 下午3:13:53
 * @author XiaoHuang
 *
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class OriginaInnerFormalGWSVehicle {

	@Autowired
	PinyiVehicleDetailedMapper pinyiVehicleDetailedMapper;

	@Autowired
	WeiZhongYunVehicleGetDataMapper weiZhongYunVehicleGetDataMapper;

	@Autowired
	WeiZhongYunVehicleDetailedMapper weiZhongYunVehicleDetailedMapper;

	// 工务署车辆项目参数主体mapper
	@Autowired
	GwsVehicleMasterDetailedMapper gwsVehicleMasterDetailedMapper;

	String shiwugaoapi_key = "d36ac3de10b24a24974fdb8972501782"; // 十五高
	String shiwugaoengCode = "SZSW-009-2020";// 工程编号
	String shiwugaoprojectCode = "XM20190707";// 项目编号

	String shisangaoapi_key = "c018ec869fdb41ae929edb355f5d0ae3"; // 十三高
	String shisangaoengCode = "562f4206-a226-468b-9c08-55161b7b3460";// 工程编号
	String shisangaoprojectCode = "XM20190708";// 项目编号

	String kejiguanapi_key = "6a488dbffd844d1ca2548280cc41a9ec"; // 科技馆
	String kejiguanengCode = "KJGXG-020-2020";// 工程编号
	String kejiguanprojectCode = "XM20170286";// 项目编号

	String dapengyiyuanapi_key = "e6fe8524145f48d4802e25f97e8d4815";// 大鹏医院
	String dapengyiyuanengCode = "DPYY-035-2020";// 工程编号
	String dapengyiyuanprojectCode = "XM20150327";// 项目编号

	String api_version = "1.0";
	String baseUlr = "http://szwb.sz.gov.cn:2018/openapi";// 请求地址

	// 上传道闸

	public void formalRequestAddBasicInfo() {
		log.info("------------formalRequestAddBasicInfo------------start!");

		// 成员方法变量声明
		/**
		 * 入参项目请求参数 (请求大循环赋参)
		 */
		List<GwsVehicleMasterDetailed> gwsVehicleMasterDetailedList = null;

		/**
		 * 入参道闸参数 （请求小循环赋参）因为一个项目有一个以上道闸设备，一对多关系
		 */
		List<PinyiVehicleDetailed> pinyiVehicleList = null;
		List<PinyiVehicleDetailed> howmanyVehicleDetailed = null;

		String currApiKey = "初始赋值api_key";
		String currEngCode = "初始赋值Eng_code";
		String currProjectCode = "初始赋值Project_code";
		String currApiVersion = "初始赋值Api_version";

		String currDeviceNo = "";
		String beatTime = "初始赋值beatTime  ";
		String Name = "初始赋值Name      ";
		String regionNo = "初始赋值regionNo  ";
		int regionType = 0;

		JSONObject body = new JSONObject();
		JSONObject param = new JSONObject();

		// 实例实际请求方法以及设置请求参数
		vehicle v = new vehicle();
		// 入参项目请求参数
		DateUtil dateUtil = new DateUtil();

		gwsVehicleMasterDetailedList = gwsVehicleMasterDetailedMapper.findAll();
		pinyiVehicleList = pinyiVehicleDetailedMapper.findAll();

		for (int i = 0; i < gwsVehicleMasterDetailedList.size(); i++) {

			currApiKey = gwsVehicleMasterDetailedList.get(i).getApi_key();
			currEngCode = gwsVehicleMasterDetailedList.get(i).getEng_code();
			currProjectCode = gwsVehicleMasterDetailedList.get(i).getProject_code();
			currApiVersion = gwsVehicleMasterDetailedList.get(i).getApi_version();
			// 入参项目请求参数
			v.setApi_key(currApiKey);
			v.setEngCode(currEngCode);
			v.setProjectCode(currProjectCode);
			v.setApi_version(currApiVersion);

			// 查询一个项目对应几个道闸
			howmanyVehicleDetailed = pinyiVehicleDetailedMapper
					.selectOneProjectHowManyByGwsProjectCode(gwsVehicleMasterDetailedList.get(i).getProject_code());

			// 入参道闸参数
			for (int j = 0; j < howmanyVehicleDetailed.size(); j++) {

				v.setBaseUlr(baseUlr);

				String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

				param.put("api_key", currApiKey);
				param.put("project_code", currProjectCode);
				param.put("eng_code", currEngCode);
				param.put("api_version", currApiVersion);
				param.put("timestamp", timestamp);

				currDeviceNo = howmanyVehicleDetailed.get(j).getDeviceNo();
				Name = howmanyVehicleDetailed.get(j).getName();
				regionNo = howmanyVehicleDetailed.get(j).getRegionNo();
				regionType = howmanyVehicleDetailed.get(j).getRegionType();

				body.put("device_no", currDeviceNo); // attention 设备号需要大于13位才能上传
				body.put("name", Name); // attention 判断唯一性 。设备唯一分辨的字段
				body.put("region_no", regionNo);
				body.put("region_type", regionType);

//				body.put("region_name", "工地"); //非必填
//				body.put("install_company", "安装公司");  
//				body.put("device_installation_date", "2020-01-01 00:00:00");
				param.put("body", body);// body

				v.addBasicInfo(param, body);
			}

		}

	}

	// 上传心跳包
	public void formalRequestHeartbeat() {
		log.info("------------formalRequestHeartbeat------------start!");
		// 成员方法变量声明
		/**
		 * 入参项目请求参数 (请求大循环赋参)
		 */
		List<GwsVehicleMasterDetailed> gwsVehicleMasterDetailedList = null;

		/**
		 * 入参道闸参数 （请求小循环赋参）因为一个项目有一个以上道闸设备，一对多关系
		 */
		List<PinyiVehicleDetailed> pinyiVehicleList = null;
		List<PinyiVehicleDetailed> howmanyVehicleDetailed = null;

		String currApiKey = "初始赋值api_key";
		String currEngCode = "初始赋值Eng_code";
		String currProjectCode = "初始赋值Project_code";
		String currApiVersion = "初始赋值Api_version";

		String currDeviceNo = "初始赋值currDeviceNo";
		String beatTime = "初始赋值beatTime";

		JSONObject body = new JSONObject();
		JSONObject param = new JSONObject();

		// 实例实际请求方法以及设置请求参数
		vehicle v = new vehicle();
		// 入参项目请求参数
		DateUtil dateUtil = new DateUtil();

		gwsVehicleMasterDetailedList = gwsVehicleMasterDetailedMapper.findAll();
		pinyiVehicleList = pinyiVehicleDetailedMapper.findAll();

		for (int i = 0; i < gwsVehicleMasterDetailedList.size(); i++) {

			currApiKey = gwsVehicleMasterDetailedList.get(i).getApi_key();
			currEngCode = gwsVehicleMasterDetailedList.get(i).getEng_code();
			currProjectCode = gwsVehicleMasterDetailedList.get(i).getProject_code();
			currApiVersion = gwsVehicleMasterDetailedList.get(i).getApi_version();
			// 入参项目请求参数
			v.setApi_key(currApiKey);
			v.setEngCode(currEngCode);
			v.setProjectCode(currProjectCode);
			v.setApi_version(currApiVersion);

			// 查询一个项目对应几个道闸
			howmanyVehicleDetailed = pinyiVehicleDetailedMapper
					.selectOneProjectHowManyByGwsProjectCode(gwsVehicleMasterDetailedList.get(i).getProject_code());
			// 入参道闸参数
			for (int j = 0; j < howmanyVehicleDetailed.size(); j++) {
				// 根据项目号查询道闸设备信息

				v.setBaseUlr(baseUlr);

				String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

				param.put("api_key", currApiKey);
				param.put("project_code", currProjectCode);
				param.put("eng_code", currEngCode);
				param.put("api_version", currApiVersion);
				param.put("timestamp", timestamp);

				// ABC123456791 xin
				// ABC1234567888

				currDeviceNo = howmanyVehicleDetailed.get(j).getDeviceNo();

				body.put("device_no", currDeviceNo); // attention 需要对应已经存在的
				body.put("beat_time", dateUtil.now());

				param.put("body", body);// body

				v.heartbeat(param, body);

			} // for---end
		}
	}

	@Test
	// 上传实时数据 代码同步定时任务
	public void formalRequestUploadData() {
		log.info("------------formalRequestUploadData------------start!");
		// 成员方法变量声明
		/**
		 * 入参项目请求参数 (请求大循环赋参)
		 */
		GwsVehicleMasterDetailed gwsVehicleMasterDetailedList = null;

		/**
		 * 入参道闸参数 （请求小循环赋参）因为一个项目有一个以上道闸设备，一对多关系
		 */
		List<PinyiVehicleDetailed> pinyiVehicleList = null;
		List<WeiZhongYunVehicleGetData> howmanyWeiZhongYunDetailed = null;
		WeiZhongYunVehicleDetailed weiZhongYunVehicleDetailed = null;

		String currApiKey = "初始赋值api_key";
		String currEngCode = "初始赋值Eng_code";
		String currProjectCode = "初始赋值Project_code";
		String currApiVersion = "初始赋值Api_version";

		String currDeviceNo = "初始赋值currDeviceNo";
		String beatTime = "初始赋值beatTime";

		JSONObject body = new JSONObject();
		JSONObject param = new JSONObject();

		// 实例实际请求方法以及设置请求参数
		vehicle v = new vehicle();
		// 入参项目请求参数
		DateUtil dateUtil = new DateUtil();
		String uploadPhotoUrl = "";
		String photoUrl = "";
		String carNumber = "";
		String deviceDataTime = "";

		JSONObject results = new JSONObject();
		String message = "";

		gwsVehicleMasterDetailedList = gwsVehicleMasterDetailedMapper.selectOneByGwsProjectCode("XM20190707");
		pinyiVehicleList = pinyiVehicleDetailedMapper.selectOneProjectHowManyByGwsProjectCode("XM20190707");

		currApiKey = gwsVehicleMasterDetailedList.getApi_key();
		currEngCode = gwsVehicleMasterDetailedList.getEng_code();
		currProjectCode = gwsVehicleMasterDetailedList.getProject_code();
		currApiVersion = gwsVehicleMasterDetailedList.getApi_version();

		DataUtil dataUtil = DataUtil.getSingleton();

		for (int h = 0; h < pinyiVehicleList.size(); h++) {
			weiZhongYunVehicleDetailed = weiZhongYunVehicleDetailedMapper
					.selectByDeviceNumber(pinyiVehicleList.get(h).getDeviceNo());

			// 每条通行记录详细
			howmanyWeiZhongYunDetailed = weiZhongYunVehicleGetDataMapper
					.selectByDeviceNumberAndData(weiZhongYunVehicleDetailed.getDeviceNumber(), dataUtil.currDate());

			for (int i = 0; i < howmanyWeiZhongYunDetailed.size(); i++) {

				// 首先确定要上传什么范围内的进出数据，根据日期查找还是
				// 对于定时推送来说，对于补充推送来说 那就selectByDate();

				log.info("------------formalRequestUploadData------------start!");

				v.setApi_key(currApiKey);
				v.setEngCode(currEngCode);
				v.setProjectCode(currProjectCode);

				v.setApi_version(gwsVehicleMasterDetailedList.getApi_version());

				v.setBaseUlr(baseUlr);

				String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

				Map imageParam1 = new HashMap<String, Object>();
				List<Map<String, Object>> imagesList = new ArrayList<Map<String, Object>>();

				param.put("api_key", currApiKey);
				param.put("project_code", currProjectCode);
				param.put("eng_code", currEngCode);
				param.put("api_version", currApiVersion);
				param.put("timestamp", timestamp);

//		body.put("device_no", "ABC1234567888");  //attention  设备号需要大于13位才能上传
//		body.put("name", "1#车道闸");  //attention  判断唯一性 。设备唯一分辨的字段
//		body.put("region_no", "2");
//		body.put("region_type", "1");

				carNumber = howmanyWeiZhongYunDetailed.get(i).getCarNumber();
				deviceDataTime = howmanyWeiZhongYunDetailed.get(i).getDeviceDataTime();

				body.put("type", 1);// √
				body.put("plate_number", carNumber);// √

				if (weiZhongYunVehicleDetailed.getDeviceName().equals("出"))
					body.put("direction", 0);// √
				if (weiZhongYunVehicleDetailed.getDeviceName().equals("进"))
					body.put("direction", 1);// √
				body.put("gate_no", howmanyWeiZhongYunDetailed.get(i).getDeviceNumber()); // √
				body.put("region_no", "2");// √
				body.put("region_type", 1);// √
				body.put("region_name", pinyiVehicleList.get(h).getName());// √
				body.put("lift_time", deviceDataTime);// √

				// 解碼成本地圖片，圖片名字為道閘名+道閘記錄時間
				ImagesAndBase64Util iu = new ImagesAndBase64Util();
				String tempData = (howmanyWeiZhongYunDetailed.get(i).getDeviceDataTime() + ".jpg").replace(":", "")
						.replace(" ", "");
				photoUrl = ("C:\\Program Files\\uploadGWSImg\\" + howmanyWeiZhongYunDetailed.get(i).getDeviceNumber()
						+ tempData);
				photoUrl = iu.DecodeBase64(howmanyWeiZhongYunDetailed.get(i).getDeviceDataImageUrl(), photoUrl);

				try {
					uploadPhotoUrl = formalRequestUploadImage(photoUrl);
				} catch (Exception e) {
					log.info("图片上传结果为空/出现异常未上传." + "车牌号：" + carNumber + ".时间：" + deviceDataTime);
					continue;
				}

				imageParam1.put("img_type", 1);// √
				imageParam1.put("img_data", uploadPhotoUrl);
				imagesList.add(imageParam1);
//		channelsList.add(channelsParam2);

				body.put("images", imagesList);
				results = v.uploadData(param, body);

				try {
					message = results.getString("message");
				} catch (Exception e) {
					log.info("上传通行信息结果为空/出现异常未上传." + "车牌号：" + carNumber + ".时间：" + deviceDataTime);
					continue;
				}

				if (message.equals("成功"))
					weiZhongYunVehicleGetDataMapper.updateOneRecordUploadByCarNumberAndDeviceDataTime(carNumber,
							deviceDataTime);
				else
					log.info("上传通行信息结果是不成功的." + "车牌号：" + carNumber + ".时间：" + deviceDataTime);
			}
		}
	}

	public String formalRequestUploadImage(String photoUrl) {

		photoUrl = photoUrl.replace("\\", "\\\\");
		vehicle v = new vehicle();
		v.setApi_key(shiwugaoapi_key);
		v.setEngCode(shiwugaoengCode);
		v.setProjectCode(shiwugaoprojectCode);
		v.setApi_version(api_version);
		v.setBaseUlr(baseUlr);
		return v.uploadImage(photoUrl);
	}

}
